package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.base.query.QueryObject;
import cn.wolfcode.car.common.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("app")
public class AppointmentServiceImpl implements IAppointmentService {
    @Resource
    private AppointmentMapper appointmentMapper;
    @Resource
    private IStatementService statementService;

    @Override
    public Appointment queryById(Long id) {
        return appointmentMapper.queryById(id);
    }

    @Override
    public TablePageInfo<Appointment> query(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<>(appointmentMapper.queryForList(qo));
    }

    @Override
    @Transactional
    public void save(Appointment appointment) {
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);
        appointmentMapper.insert(appointment);
    }

    @Override
    @Transactional
    public void update(Appointment appointment) {
        Appointment oldObj = queryById(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus()))
            throw new BusinessException("只有状态为预约中的预约单才能编辑");
        appointmentMapper.update(appointment);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        Appointment appointment = queryById(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("只有状态为预约中的预约单才能删除");
        }
        appointmentMapper.softDelete(id, Appointment.DELETEFLAG_OFF);
    }

    @Override
    public void changStatus(Long id, Integer status) {
        appointmentMapper.changStatus(id, status);
    }

    @Override
    public Long generateStatement(Long id) {
        Appointment appointment = this.queryById(id);
        // 合理性判断(到店 || 结算)
        if (!(Appointment.STATUS_ARRIVAL.equals(appointment.getStatus()) || Appointment.STATUS_SETTLE.equals(appointment.getStatus())))
            throw new BusinessException("只有状态为已到店或已结算的预约单才能结算单");
        this.changStatus(id, Appointment.STATUS_SETTLE);
        // 判断是否有记录
        Statement statement = statementService.queryAppointmentId(id);
        if (statement == null) {
            statement = new Statement(){
                {
                    this.setCustomerName(appointment.getCustomerName());
                    this.setCustomerPhone(appointment.getCustomerPhone());
                    this.setActualArrivalTime(appointment.getActualArrivalTime());
                    this.setLicensePlate(appointment.getLicensePlate());
                    this.setCarSeries(appointment.getCarSeries());
                    this.setServiceType((long) appointment.getServiceType());
                    this.setAppointmentId(appointment.getId());
                    this.setInfo(appointment.getInfo());
                }
            };
            statementService.save(statement);
        }
        return statement.getId();
    }
}
